DS4PS


This lab demonstrates the use of base maps to provide context for geographic point data. We will utilize map tiles from the ggmap package and an interactive HTML map using the leaflet package.

You can create a new RMarkdown file, or download the lab template:

RMD Template



Data

For this lab we will focus on accidents that result in injuries or fatalities:



Maps in R

This lab will introduce you to two mapping functions that allow you to add base tiles underneath your plotting points to provide context for the data.

For these examples we will use the count of injuries and fatalities to scale point sizes on the graphics:

The main thing to pay attention to across the various mapping programs is that they all have similar aesthetic capabilities, but the arguments will be slightly different in each package:

ARGUMENT: plot() qmplot() leaflet()
COLOR col=“red” color=I(“red”) fillColor=“red”
POINT SIZE cex=point.size size=I(point.size) radius=20*point.size
TRANSPARENCY col=alpha(“red”,alpha=0.5) alpha=I(0.1) fillOpacity=0.5

Let’s start by plotting the accidents in the dataset using the Longitude and Latitude coordinates:

We see nice patterns emerge and the outlines of streets, but this data is hard to interpret without a little context. We can easily add context by including an underlying map tile to show streets and landmarks behind the data. The qmplot() function makes this very easy:

The qmplot() functions includes some nice features for summarizing dense point data to help find patterns. We will specifically look at the heat map features and the facet_wrap() option.

Adding the arguments geom=“density2d” will transform the data from points on a map to a contour plot that represents density of points. It can be a helpful function for summarizing dense data.

There are lots of ways to customize the data using ggmap:

The facet_wrap() option allows us to quickly create multiple plots with subsets of the data using a categorical variable in the dataset:

Table continues below
10 ANGLE (Front To Side)(Other Than Left Turn) Head On Left Turn Other Rear End
1 1837 142 2171 608 2932
Table continues below
Rear To Rear Rear To Side Sideswipe Opposite Direction
8 10 29
Sideswipe Same Direction Single Vehicle Unknown
327 541 38

Leaflet Maps

The leaflet map operates a little differently. It is a dynamic map, so the user can zoom in or scroll to various positions on the map so you do not have to set a specific zoom level beforehand.

Lab Instructions

In this lab you will look for patterns in the data, and highlight those patterns either by creating side-by-side maps to compare data subsets, or else by color-coding specific cases and providing details of the crash as a pop-up window.

All of the solutions will involve re-using code from the map examples above.



PART 1: qmplot()

1) Compare two-car accidents to those involving pedestrians or bikers.

Write a logical statement where TRUE specifies any accident that involves a pedestrian or cyclist.

Note that the variables “Unittype_One” and “Unittype_Two” report whether a pedestrian or cyclist was involved.

Use this new logical vector as the categorical variable in the facet_wrap() function to compare the location patterns of accidents involving cars to those involving pedestrians or cyclists.

Include your maps and report whether location patterns differ.


2) Compare accidents involving drugs and alcohol to those that don’t.

Similar to above, define a logical vector where TRUE represents an accident where either driver was under the influence of drugs or alcohol. Use facet_wrap() to compare the location of sober accidents to those where at least one driver was impaired.

Include your maps and report whether location patterns differ.



PART 2: leaflet()

This example demonstrates how to highlight accidents where fatalities occurred. Can you find the deadliest intersection in Tempe on this map?


1) Compare two-car accidents to those involving pedestrians or bikers.

Similar to the leaflet example above, use a logical statement to identify all accidents involving pedastrians and cyclists. Highlight those cases involving pedestrians or cyclists with a bright color, and use some shade of gray for other accidents.


2) Compare accidents involving drugs and alcohol to those that don’t.

Highlight cases that involve drugs and alcohol. Include the age of both drivers in the pop-up.



Submission Instructions

After you have completed your lab, knit your RMD file. Login to Canvas at http://canvas.asu.edu and navigate to the assignments tab in the course repository. Upload your RMD and your HTML files to the appropriate lab submission link.

Remember to: